<?php
/**
 * UserModel
 * @author tuanna
 * @version 1.0
 */
require_once 'Zend/Db/Table/Abstract.php';


/**
 * UserModel
 * @author tuanna
 * データベースのユーザテーブルを処理 
 */
class UserModel extends Zend_Db_Table_Abstract
{
    /**
     * データベース名前
     */
    protected $_name = 'user';
    
    
    /**
     * addUser 
     * 新記録を挿入します
     *   - $dataに新記録の情報を挿入します
     *   - insertファンクションで新記録を挿入します
     * 
     * @param $username
     * @param $password
     * @param $type
     * @param $name
     * @param $email
     * @param $phone
     */
    public function addUser($username, $password, $type, $name, $email, $phone = NULL, $address = ''){
    	$data = array(
    		'username' => $username,
    		'password' => $password,
    		'type' => $type,
    		'name' => $name,
    		'email' => $email,
    		'phone' => $phone,
    		'address' => $address
    	);
    	
    	return $this->insert($data);
    }
    
    public function addUserData($data){
    	return $this->insert($data);
    }
    
    /**
     * updateUser
     * ユーザの情報を変更します。
     * 	- $dataに変更の情報を挿入します
     * 	- updateファンクションで変更します
     * 
     * @param $id
     * @param $username
     * @param $password
     * @param $type
     * @param $name
     * @param $email
     * @param $phone
     */
    public function updateUser($id, $username, $password, $type, $name, $email, $phone, $address = ''){
    	$data = array(
    		'username' => $username,
    		'password' => $password,
    		'type' => $type,
    		'name' => $name,
    		'email' => $email,
    		'phone' => $phone,
    		'address' => $address
    	);
    	$check = $this->update($data, "id = $id");
    	
    	return true;
    }
    
    public function updateUserData($id, $data){
    	$check = $this->update($data, "id = $id");
    	
    	return $check;
    }
    
    /**
     * deleteUser
     * ユーザの情報を削除します
     * 	- ユーザの記録を削除しません、記録のステータスを変更します STATUS_DELETED = 0 
     *	- $del_idsは削除ユーザイドのリストです、カンマで区切り 。例： 1,2,3
     * 
     * @param $del_ids
     */
    public function deleteUser($del_ids){
    	if($this->update(array('status' => 0), "id in ($del_ids)") > 0){
    		
    		return true;
    	}else{
    		
    		return false;
    	}
    }
    
    /**
     * データベースからユーザのリストを引き出します。 
     * 
     * @param $search_type 
     * @param $search_key
     * @param $page
     * @param $line_per_page
     */
    public function getListUser($search_type = "all", $search_key="", $page = NULL, $line_per_page= NULL, $status = 0, $type = 0){
    	$db = $this->getAdapter();
    	$select = $this->select();
    	
    	//search by search_type & search_key
    	if($search_key){
	    	if($search_type == "all")
	    		$select->where("username like '%$search_key%' or name like '%$search_key%' or email like '%$search_key%' or phone like '%$search_key%'");
	    	
	    	else if ($search_type == "username")
	    		$select->where("username like '%$search_key%'");
	    	
	    	else if ($search_type == "name")
	    		$select->where("name like '%$search_key%'");
	    	
	    	else if ($search_type == "phone")
	    		$select->where("phone like '%$search_key%'");
    	}
    	
    	if($status == 0)
			$select->where('status > 0');
		else
			$select->where('status = ?', $status);
    	
		if($type > 0)
			$select->where('type = ?', $type);
			
    	//do paging
    	if($page && $line_per_page){
    		$select->limitPage($page, $line_per_page);
    	}
    	
    	//order
    	$select->order("status desc");
    	$select->order("username");
    	
    	$list = $db->fetchAll($select);
    	
    	return $list;
    }
  
    /**
     * getListUserCount
     * Phương thức tính số bản ghi thỏa mãn điều kiện cho trước.
     * Phục vụ cho mục đích phân trang dữ liệu
     *  
     * @param $search_type
     * @param $search_key
     */
    public function getListUserCount($search_type = "all", $search_key="", $status = 0, $type = 0)
    {
    	$db = Zend_Registry::get('connectDB');
    	$select = $this->select();
    	
    	if($search_key){
	    	if($search_type == "all")
	    		$select->where("username like '%$search_key%' or name like '%$search_key%' or email like '%$search_key%' or phone like '%$search_key%'");
	    	
	    	else if ($search_type == "username")
	    		$select->where("username like '%$search_key%'");
	    	
	    	else if ($search_type == "name")
	    		$select->where("name like '%$search_key%'");
	    	
	    	else if ($search_type == "phone")
	    		$select->where("phone like '%$search_key%'");
    	}
    	
		if($status == 0)
			$select->where('status > 0');
		else
			$select->where('status = ?', $status);
    	
		if($type > 0)
			$select->where('type = ?', $type);
			
    	$list = $db->fetchAll($select);
    	
    	return count($list);
    }    	
    
    /**
     * fetchUser
     * Lấy thông tin một user theo id
     * 
     * @param unknown_type $id
     */
  	public function fetchUser($id){
    	$row = $this->fetchRow("id = $id and status = 1");
    	return $row;
    }
	
	
	 /**
     * getUserID
     * Phương thức để lấy ID khi biết username.
     * Phục vụ cho mục đích thêm vào bảng order
     *  
     * @param $search_key
     */
	public function getUserID ($search_key){
		$db = Zend_Registry::get('connectDB');
		$row = $db->fetchRow("username = '{$search_key}' and status = 1");
		$id= $row['id'];
		return $id;
		/*$user = $this->fetchAll("username like '%$search_key%' and status = 1");
		if(count($user)>0){
			foreach ($user as $one){
				$arrUid[] = $one['id'];
			}
		}
		
		return $arrUid;*/
		
	}
	
	public function isUsernameExist ($username) 
	{
		$row = $this->fetchRow("username = '$username' and status > 0");
		if ($row)
			return true;
		else
			return false;
	}
	
	public function isEmailExist ($email)
	{
		$row = $this->fetchRow("email = '$email' and status > 0");
		
		if ($row)
			return true;
		else
			return false;
	}
	
	public function getUserInfoByEmail($email){
		$select = $this->select();
		
		$select->where("email = ?", $email);
		$select->where("status = ?", 1);
		
		$row = $this->fetchRow($select);
		
		return $row;
	}
	
	public function resetpass ($email)
	{
		$row = $this->fetchRow("email = '$email' and status = 1");
		$random_number = rand(100000,999999);
		$pass = hash("SHA1",$random_number);
		$data = array(
			'password'=>$pass
		);
		
		if($row){
			$id= $row->id;
			$check = $this->update($data, "id = $id");
			if($check){
				$account_info = array('new-pass'=>$random_number,
										'username'=>$row['username'],
										'email' => $row['email'],
										'name' => $row['name']);
				return $account_info;
			}
		}else{
			return false;
		}
	}
}